package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.data.general.RowBase;
import com.szholly.pro.watergas_server.ps.android.common.ResultUtils;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.service.CommonAndroidService;
import com.szholly.utils.util.StringUtils;
import org.activiti.engine.impl.util.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import net.sf.json.JSONObject;

/**
 * 公共接口
 * 
 * 1.登录接口
 * 
 * 2.获取模块列表
 * 
 * @author joey
 *
 */
@Controller
@RequestMapping("/android/Common/*")
public class AndroidCommonController {
	@Autowired
	CommonAndroidService commonService;

	AndroidCommonManager mCommonManager = AndroidCommonManager.get();

	/**
	 * 获取登录信息
	 */
	@RequestMapping("/getLoginData")
	@ResponseBody
	public String getLoginData(String token, String userName,String password) throws Exception {
		try{
			return mCommonManager.getLoginData(userName, password);
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	
	/**
	 * 获取模块数据
	 */
	@RequestMapping("/getModuleData")
	@ResponseBody
	public String getModuleData(String token, String userId) throws Exception {
		if (StringUtils.IsNullOrSpace(userId)) {
			return new JSONArray().toString();
		}		
		return mCommonManager.getModuleData(userId);
	}
	
	/**
	 * 获取模块数据
	 */
	@RequestMapping("/savePhoneSystemData")
	@ResponseBody
	public String savePhoneSystemData(String token, String userId,String deviceCode,String data){
		try{
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userId,"用户编号不能为空");
			ValidUtils.hasText(deviceCode,"手机编号不能为空");
			ValidUtils.hasText(data,"数据不能为空");
			return mCommonManager.savePhoneSystemData(userId,deviceCode,data);			
		}catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}		
	}

	/**
	 * 
	 */
	@RequestMapping("/addOperationTask")
	@ResponseBody
	public String addOperationTask(String token,String userId, String stationCode,String jobCode,String taskCodePrefix) {
		try {
			ValidUtils.validateToken(token);
			RowBase rowBase = mCommonManager.addOperationTask(userId, stationCode, jobCode, taskCodePrefix);
			return ResultUtils.getToastResult(rowBase);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	/**
	 * 
	 */
	@RequestMapping("/deleteOperationTask")
	@ResponseBody
	public String deleteOperationTask(String token,String userId, String taskCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userId,"用户编号为空,运维任务删除失败");
			ValidUtils.hasText(taskCode,"任务编号为空,运维任务删除失败");
			mCommonManager.deleteOperationTask(taskCode);
			return ResultUtils.getToastResult("删除成功",true);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	@RequestMapping("/getOperationTaskList")
	@ResponseBody
	public String getOperationTaskList(String token,String userId, String stationCode, String jobCode) {
		try {
			ValidUtils.validateToken(token);
			List<RowBase> list = mCommonManager.getOperationTaskList(userId, stationCode, jobCode);			
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}
	
	/**
	 * 登录接口
	 */
	@RequestMapping("/login")
	@ResponseBody
	public String login(String userName, String password) throws Exception {
		String loginResult = commonService.login(userName, password);
		return loginResult;
	}

	/**
	 * 获取所有运维类型(包括污水厂、供水、地表水、工业污染)
	 * 
	 * @param userID
	 * @param type
	 * @return
	 */
	@RequestMapping("/getAllTaskType")
	@ResponseBody
	public String getAllTaskType(String userID, String type, String groupID) throws Exception {
		String jsonResult = commonService.getAllTaskType(userID, type, groupID);
		return jsonResult;
	}

	/**
	 * 生成任务
	 * 
	 * @param row
	 * @return
	 */
	@RequestMapping("/generateTask")
	@ResponseBody
	public String generateTask(String row) {
		String jsonResult = commonService.generateTask(row);
		return jsonResult;
	}

	/**
	 * 获取签到信息
	 * 
	 * @param userID
	 * @param type
	 * @return
	 */
	@RequestMapping("/getSignIn")
	@ResponseBody
	public String getSignIn(String userId, String scId, String type) {
		String jsonResult = commonService.getSignIn(userId, scId, type);
		return jsonResult;
	}

	/**
	 * 保存任务
	 * 
	 * @param
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(String token, String userID, String tableName, String row, String dataType, String TASK_CODE,
			String CREATE_TIME, String FINISH_BY) throws Exception {
		String jsonResult = commonService.save(token, userID, tableName, row, dataType, TASK_CODE, CREATE_TIME,
				FINISH_BY);
		return jsonResult;
	}

	/**
	 * 获取定期维护运维项
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDqwhItems")
	@ResponseBody
	public String getDqwhItems(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String jsonResult = commonService.getDqwhItems(resq, res);
		return jsonResult;
	}

	/**
	 * 批量数据保存
	 * 
	 * @param tableName
	 *            数据表名
	 * @param rows
	 *            记录集，Json格式数组
	 */
	@RequestMapping("/saveList")
	@ResponseBody
	public String saveList(String tableName, String rows, String dataType, String TASK_CODE, String CREATE_TIME,
			String FINISH_BY) {
		String jsonResult = commonService.saveList(tableName, rows, dataType, TASK_CODE, CREATE_TIME, FINISH_BY);
		return jsonResult;
	}

	/**
	 * 获取运维组人员信息
	 * 
	 * @param exp_no
	 * @return
	 */
	@RequestMapping("/getGroupUser")
	@ResponseBody
	public String getGroupUser(String exp_no) {
		String jsonResult = commonService.getGroupUser(exp_no);
		return jsonResult;
	}

	/**
	 * android获取参数单位
	 * 
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getUnits")
	@ResponseBody
	public String getUnit() {
		String jsonResult = commonService.getUnit();
		return jsonResult;
	}

	/**
	 * 登录时获取字典的值
	 */
	@RequestMapping("/getDataDefulatdomain")
	@ResponseBody
	public String getDataDefulatdomain(String S_DOMAINNAME) {
		String jsonResult = commonService.getDataDefulatdomain(S_DOMAINNAME);
		return jsonResult;
	}

	// 获取版本号
	@RequestMapping("/updateno")
	@ResponseBody
	public String getVersionNo() {
		String jsonResult = commonService.getVersionNo();
		return jsonResult;
	}

	/**
	 * 删除创建未完成的任务
	 * 
	 *
	 */
	@RequestMapping("/delUndoneGenerate")
	@ResponseBody
	public String delUndoneGenerate(String TASK_CODE) throws Exception {
		String jsonResult = commonService.delUndoneGenerate(TASK_CODE);
		return jsonResult;
	}

	/**
	 * 查看运维项总未完成任务数 0污水 1地表水
	 * 
	 */
	@RequestMapping("/getOperationTaskNum")
	@ResponseBody
	public String getOperationTaskNum(String factoryId,String type) {
		String jsonResult = commonService.getOperationTaskNum(factoryId,type);
		return jsonResult;
	}

	/**
	 * 
	 */
	@RequestMapping("/getOperationTask")
	@ResponseBody
	public String getTaskList(String jobId, String stationId) {
		String jsonResult = null;
		if (jobId != null && jobId.equals("123456")) {
			jsonResult = commonService.getAbnormalDisposalData(jobId, stationId);
		} else {
			jsonResult = commonService.getTaskList(jobId, stationId);
		}
		return jsonResult;
	}
}
